﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.Commands;

namespace StockManagement.ViewModels
{
    public class NewMaterialViewModel : MaterialViewModel
    {
        DelegateCommand _addNewCommand;
        DelegateCommand _discardCommand;
        string _materialName;
                
        public delegate void MaterialAddedDelegate();
        public event MaterialAddedDelegate MaterialAdded;

        public NewMaterialViewModel():base()
        {
            _addNewCommand = new DelegateCommand(ExecuteAddCommand, CanExecuteCommand);
            _discardCommand = new DelegateCommand(ExecuteDiscardCommand, CanExecuteDiscardCommand);
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(NewMaterialViewModel_PropertyChanged);
            _materialName = base.MaterialName;
        }

        void NewMaterialViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaiseCanExecuteCommands();
        }

        public string NewMaterialName
        {
            get { return _materialName; }
            set
            {
                if (ValidateMaterialName(value))
                    _materialName = value;
                else
                    ShowErrorMessageBox("The Material name already exists", "Duplicate Name");
                OnPropertyChanged("NewMaterialName");
            }
        }

        private void RaiseCanExecuteCommands()
        {
            AddNewCommand.RaiseCanExecuteChanged();
            DiscardCommand.RaiseCanExecuteChanged();
        }

        public DelegateCommand AddNewCommand
        {
            get { return _addNewCommand; }
        }

        public DelegateCommand DiscardCommand
        {
            get { return _discardCommand; }
        }

        private bool CanExecuteCommand(object parameter)
        {
            if (NewMaterialName.Trim().Length > 0 && MaterialCode.Length > 0)
                return true;
            return false;
        }

        private bool CanExecuteDiscardCommand(object parameter)
        {
            if (NewMaterialName.Trim().Length > 0 || MaterialCode.Length > 0)
                return true;
            return false;
        }

        private void ExecuteDiscardCommand(object parameter)
        {
            string strmessage = string.Format("Do you want to discard the changes to  material?");
            var result = ShowConfirmationMessageBox(strmessage, "Discard Changes");
            if (!result)
                return;

            DiscardMaterial();
            RaiseCanExecuteCommands();
        }

        private void ExecuteAddCommand(object parameter)
        {
            string strmessage = string.Format("Do you want to add the new Material '{0}'?", MaterialName);
            var result = ShowConfirmationMessageBox(strmessage, "Add Material");
            if (!result)
                return;
            MaterialName = NewMaterialName;
            if (SaveMaterial() && MaterialAdded != null)
                MaterialAdded();
            RaiseCanExecuteCommands();
        }
    }
}
